package com.koron.car.web.authorized.controller;

import com.alibaba.fastjson.JSON;
import com.koron.bean.base.Response;
import com.koron.car.web.authorized.bean.entity.CarNumberBind;
import com.koron.car.web.authorized.bean.vo.CarDelimitMsgVO;
import com.koron.car.web.authorized.service.CarDelimitService;
import com.koron.common.core.config.ThreadLocalContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/delimit")
@Api(tags = "车辆编制-定编")
public class DelimitController {

    @Autowired
    private CarDelimitService delimitService;

    @GetMapping("/getDelimitNum")
    @ApiOperation("根据条件获取定编号")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "编制类型，prepare_type(delimit_manager,delimit_security)"),
            @ApiImplicitParam(name = "state", value = "绑定状态，（unbounded，binding，replacement）")
    })
    public Response<List<CarDelimitMsgVO>> getDelimitNum(@RequestParam(required = false) String type, @RequestParam(required = false) String state) {
        log.info("获取所有未绑定的定编号, type = {}", type);
        List<CarDelimitMsgVO> delimits = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), delimitService, "getDelimitNum", List.class, type, state);
        return Response.ok(delimits);
    }

    @PostMapping("/bind")
    @ApiOperation("车辆编号绑定")
    public Response<CarNumberBind> bind(@RequestBody CarNumberBind bind) {
        log.info("车辆编号绑定, bind = {}", JSON.toJSONString(bind));
        CarNumberBind delimits = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), delimitService, "bind", CarNumberBind.class, bind);
        return Response.ok(delimits);
    }

    @GetMapping("/getBindById")
    @ApiOperation("获取绑定信息")
    public Response<CarNumberBind> getBindById(String bindId) {
        log.info("获取绑定信息, bindId = {}", bindId);
        CarNumberBind delimits = ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), delimitService, "getBindById", CarNumberBind.class, bindId);
        return Response.ok(delimits);
    }
}
